﻿<?php
session_start();
ini_set('max_execution_TIME', 600);
include("dbconfig.php");

$db = mysql_pconnect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
mysql_query("SET names UTF8");
mysql_select_db($database) or die("Error conecting to db.");
$examp = $_REQUEST["q"]; 
$ip=$_REQUEST["ip"];

$page = $_REQUEST['page']; 
$limit = $_REQUEST['rows']; 
$sidx = $_REQUEST['sidx']; 
$sord = $_REQUEST['sord']; 
$userid=$_SESSION['userid'];
$sign=$_SESSION['sign'];
if(!$sidx) $sidx =1;
$style=-1;

if(isset($_GET["style"]))
{
$style = $_GET['style'];
}
else{
$style= -1;
}
if(isset($_GET["sip_mask"]))
{
$sip_mask = $_GET['sip_mask'];
}
else{
$sip_mask= '';
}
if(isset($_GET["from_mask"]))
{
$from_mask = $_GET['from_mask'];
}
else{
$from_mask = '';
}
if(isset($_GET["to_mask"]))
{
$to_mask = $_GET['to_mask'];
}
else{
$to_mask = '';
} 


if($style==-1){
	echo "empty";
}
else if($style==0){

	//$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());

	$where = " WHERE 1=1 ";
	if($sip_mask!='') $where.= " AND ip like '$sip_mask%'";
	$from_mask=strtotime($from_mask);
	$to_mask=strtotime($to_mask);
	//echo $to_mask;

	$db_table = mysql_list_tables($database);
	$target_table=array();
	while($row = mysql_fetch_row($db_table)){
		$tb_name=$row[0];
		$tb_time=explode('_', $tb_name);
		$tb_time=strtotime($tb_time[1]);
		if($tb_time>=$from_mask){
			//echo $tb_name."</br>";
			$target_table[] = $tb_name;
			break;
		}	
	}
	foreach($target_table as &$tb_name){
	$SQL ="select count(*) as c from ".$tb_name." ".$where;

	
	$result = mysql_query($SQL);
	if (!$result) echo "SQL ERROR：".mysql_error();
	$count = mysql_fetch_array($result,MYSQL_ASSOC);
	$count=$count["c"];
	if( $count >0 ) {
		$total_pages = ceil($count/$limit);
	} 
	else {
		$total_pages = 0;
	}
	if ($page > $total_pages) $page=$total_pages;
	if ($limit<0) $limit = 0;
	$start = $limit*$page - $limit; // do not put $limit*($page - 1)
	if ($start<0) $start = 0;
	$SQL ="select tid,ip,port,code,header,time from ".$tb_name." ".$where." ORDER BY $sidx $sord LIMIT $start,$limit";
	$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());
	$response->page = $page;
	$response->total = $total_pages;
	$response->records = $count;
	$i=0;

	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		$response->rows[$i]['ID']=$row[tid];
		$response->rows[$i]['cell']=array($row[tid],$row[ip],$row[port],$row[code],$row[header],$row[time],"",$tb_name);
		$i++;
	} 
	}
	echo json_encode($response);
	//mysql_close($db);
	//echo json_encode($responce);
}

else if($style==1)
{
	
	$table=$from_mask;
	$ip=$sip_mask;
	$SQL ="select * from ".$table." where ip like '".$ip."'";
	$result = mysql_query($SQL);
	if (!$result) echo "SQL错误：".mysql_error();
	$row = mysql_fetch_array($result,MYSQL_ASSOC);
	$response=json_encode($row[content]);
	echo $response;
	//mysql_close($db);
} 
else if($style==2)
{

	$where = " WHERE ip like '".$sip_mask."'";
	$db_table = mysql_list_tables($database);
	$target_table=array();
	while($row = mysql_fetch_row($db_table)){
		$tb_name=$row[0];
		$pos = strpos($tb_name, "scanner_");
		if($pos!==false){
			$target_table[] = $tb_name;
		}
	}

	$i=0;
	foreach($target_table as &$tb_name)
	{
		$SQL ="select tid,ip,port,code,header,time from ".$tb_name." ".$where;
		$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());	
	  	$row = mysql_fetch_array($result,MYSQL_ASSOC);
		$response->rows[$i]['ID']=$row[tid];
		$response->rows[$i]['cell']=array($row[tid],$row[ip],$row[port],$row[code],$row[header],$row[time],"",$tb_name);
		$i++;
		if($i==6){
			break;
		}

	}
	$count=$i;
	if( $count >0 ) {
		$total_pages = ceil($count/$limit);
	} 
	else {
		$total_pages = 0;
	}
	if ($page > $total_pages) $page=$total_pages;
	$response->page = $page;
	$response->total = $total_pages;
	$response->records =$count ;
	echo json_encode($response);

}
else if($style==3)
{
$where = " WHERE 1=1  ";
if($rev_mask!='')
	$where.= " AND receiver like '$rev_mask'";
#if($type_mask!='')
#	$where.= " AND type ='$type_mask'";


$link = mysql_connect($dbhost,$dbuser,$dbpassword) or die('Unable to establish a DB connection');
mysql_select_db($database,$link);
mysql_query("SET names UTF8");
date_default_timezone_set($timezone); //北京时间
$ret=array();
$SQL="select id,count(*) as num,dip,sip,date from res_email ".$where." group by date";


$result = mysql_query($SQL); 
if (!$result) echo "SQL错误：".mysql_error();

while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$t_date=$row[date];


$x=strtotime($t_date)*1000;
$y1=intval($row[num]);
$y2=$row[sip];
$tmp=array($x,$y1);
array_push($ret,$tmp);
}

//$ret=array(array(1318253100000,100),array(1318253100029,200),array(1318253100040,1));
//$ret=array(array(1337084640000,1),array(1337084640000,2),array(337085300000,1));
echo json_encode($ret);

}
else if($style==4){
$db = mysql_pconnect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());   
$SQL="update res_email set iswhite=1,type=0 where receiver='$rev_mask' ";    
mysql_select_db($database) or die("Error conecting to db.");
$result = mysql_query($SQL);
if (!$result) echo "SQL错误：".mysql_error();

$SQL ="select * from white_email where email like '$rev_mask' ";
$result = mysql_query($SQL);
if (!$result) echo "SQL错误：".mysql_error();
$count = mysql_num_rows($result);
if($count==0)
{
	$SQL="Insert into white_email (userid,sign,email) values ('$userid','$sign','$rev_mask') "; 
	$result = mysql_query($SQL);
	if (!$result) echo "SQL错误：".mysql_error();
}
else{
	echo "已经存在";
}
}


?>
